/*---------------------------------------------------------------------------
 *-Version :0.1 @ 09/01/2008                                                *
 *-Auther : Chen Ju                                                         *
 *-Auther Eail: sammy105@gmail.com                                         *
 *-Auther site : http://www.kylogs.co/blog                                 * 
 *-This file defines all vector functions                                   * 
 *---------------------------------------------------------------------------
 */
#include"easyc_ds.h"
#include<stdlib.h>
#include<stdio.h>
/* function creats a vector with given size (though the given size doesn't limit the actural size of a vector )  */
c_vector make_vector(int size){
	c_vector s;
	s.size=0;
	s.base_size=20;
	s.actual_size=size;
	s.base=(eType *)malloc(sizeof(eType)*size);
	s.get=easyc_vector_get;
	s.set=easyc_vector_set;
	s.remove=easyc_vector_remove;
	s.sort=easyc_vector_sort;
	s.add=easyc_vector_add;
	return s;
}
/* function returns the value of elment at offset i */
eType easyc_vector_get(int i,const struct easyc_vector *s){
	eType t;
	if(i>=s->size) {
		printf("Vector out of bund, the return value will be random ");
		return t;
	}
	return *(s->base+i);
		
}
/* function set the value of offset i in the vector to be e  */
void easyc_vector_set(int i,eType e, const struct easyc_vector *s){
	if(i>=s->size) {
		printf("Vector out of bound,nothing changed to the vector ");
		return ;
	}
	*(s->base+i)=e;
	return;
}
/* to be a vector, maybe remove function doesn't make much sense, anyway so it's still be supported.  */
eType easyc_vector_remove(int i,struct easyc_vector *s){
	eType t;
	int j=0;
	if(i>=s->size) {
		printf("Vector out of buond, nothing changed, and return value is random");
		return t;
	}
	t=*(s->base+i);
	for(j=i;j<s->size-1;j++){
		*(s->base+j)=*(s->base+j+1);
	}
	s->size=s->size-1;
	if(s->size>=s->base_size){
		free(s->base+i);
	}
	return t;
}
/* function adds new element to vector */
void easyc_vector_add(eType e, c_vector *s){
	if(s->actual_size==s->size){
		s->base=(eType *)realloc(s->base,sizeof(eType)*(s->actual_size+s->base_size));
		s->actual_size=s->actual_size+s->base_size;
	}
	s->size=s->size+1;
	*(s->base+s->size-1)=e;
	return;
}
void easyc_vector_sort(int(*compare)(const void *a ,const void *b),c_vector *s){
	qsort(s->base,s->size,sizeof(eType),compare);
	return;
}
